package com.foo.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.example.www.FacadeInterfaceServiceStub.GetRestaurantInfo1ByRatingFault;
import org.example.www.FacadeInterfaceServiceStub.GetRestaurantInfoByCusineFault;
import org.example.www.FacadeInterfaceServiceStub.GetRestaurantInfoByNameFault;
import org.example.www.FacadeInterfaceServiceStub.RestaurantFacade;
import org.example.www.FacadeInterfaceServiceStub.RestaurantFacadeList;
import org.example.www.FacadeWSClient;
import org.example.www.GetRestaurantInfo1ByRatingFaultException;
import org.example.www.GetRestaurantInfoByCusineFaultException;
import org.example.www.GetRestaurantInfoByNameFaultException;


/**
 * Servlet implementation class RestaurantFacadeServlet
 */
public class RestaurantFacadeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RestaurantFacadeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		String sortBased = request.getParameter("SortBased");
		String paramName = "";
		RestaurantFacadeList restaurantarray = null;
		FacadeWSClient client = new FacadeWSClient();
		RequestDispatcher dispatcher = null;
		
		if(sortBased.equalsIgnoreCase("resta_name"))
		{
			paramName = request.getParameter("restaurant_name");
			RestaurantFacade res = null;
			try {
				res = client.getRestaurantInfoByName(paramName);
			} catch (GetRestaurantInfoByNameFaultException e) {
				GetRestaurantInfoByNameFault fault = e.getFaultMessage();
				String exceptionString = fault.getGetRestaurantInfoByNameFault();
				
				request.setAttribute("ServerMessage",exceptionString);
				dispatcher = getServletContext().getRequestDispatcher("/restaurant.jsp");
				dispatcher.forward(request, response);
			}
			restaurantarray = new RestaurantFacadeList();
			restaurantarray.addRestaurantList(res);
		}
		else if(sortBased.equalsIgnoreCase("cuisin"))
		{
			paramName = request.getParameter("cuisine");
			
			try {
				restaurantarray = client.getRestaurantInfoByCuisine(paramName);
			} catch (GetRestaurantInfoByCusineFaultException e) {
				GetRestaurantInfoByCusineFault fault = e.getFaultMessage();
				String exceptionString = fault.getGetRestaurantInfoByCusineFault();
				
				request.setAttribute("ServerMessage",exceptionString);
				dispatcher = getServletContext().getRequestDispatcher("/restaurant.jsp");
				dispatcher.forward(request, response);
			}
		}
		else if(sortBased.equalsIgnoreCase("resta_rating"))
		{
			paramName = request.getParameter("restaurant_rating");
			
			//Convert to double
			double rating = Double.parseDouble(paramName);
			
			try {
				restaurantarray = client.getRestaurantInfoByRating(rating);
			} catch (GetRestaurantInfo1ByRatingFaultException e) {
				GetRestaurantInfo1ByRatingFault fault = e.getFaultMessage();
				String exceptionString = fault.getGetRestaurantInfo1ByRatingFault();
				
				request.setAttribute("ServerMessage",exceptionString);
				dispatcher = getServletContext().getRequestDispatcher("/restaurant.jsp");
				dispatcher.forward(request, response);
			}
		}

		session.setAttribute("RestaurantData", restaurantarray.getRestaurantList());
	      
	    dispatcher = getServletContext().getRequestDispatcher("/restaurant.jsp");
		dispatcher.forward(request, response);
	}

}
